package com.gxuwz.medical.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.join_nh.Join_nh;
import com.gxuwz.medical.exception.DbException;
import com.gxuwz.medical.tool.MyTime;

public class Join_nhDao extends GenericDao<Join_nh> {

	@Override
	protected Join_nh handle(ResultSet rs) throws SQLException {
		String join_nh_code = rs.getString("join_nh_code");
		String family_id = rs.getString("family_id");
		String join_id_card = rs.getString("join_id_card");
		String join_invoice_code = rs.getString("join_invoice_code");
		String money = rs.getString("money");
		Timestamp join_time = rs.getTimestamp("join_time");
		String user_id = rs.getString("user_id");
		String areacode = rs.getString("areacode");
		String join_year = rs.getString("join_year");

		Area area = new Area(areacode);
		Family family = null;
		try {
			family = new Family(family_id);
		} catch (DbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Join_nh entity = new Join_nh(join_nh_code, family_id, join_id_card, join_time, join_invoice_code, money, area,
				user_id, join_year, family);
		return entity;
	}

	public List<Join_nh> findFamilyJoin_nhList() throws DbException {
		try {
			String sql = "select * from tb_join_nh where 1=1 and join_year='" + MyTime.getStringNowYear() + "'";

			Object[] params = {};

			return super.queryOjects(sql, params);

		} catch (SQLException e) {
			throw new DbException("按级别查找行政区域失败!", e);
		}
	}

	public List<Join_nh> findJoin_nhList(String join_id_card) throws DbException {
		try {
			String sql = "select * from tb_join_nh where 1=1 and join_id_card = '" + join_id_card + "' and join_year='"
					+ MyTime.getStringNowYear() + "'";

			Object[] params = {};

			return super.queryOjects(sql, params);

		} catch (SQLException e) {
			throw new DbException("按级别查找行政区域失败!", e);
		}
	}

	public List<Join_nh> findByLike(String where) throws DbException {
		try {

			String sql = "select * from tb_join_nh where 1=1 " + where + " ORDER BY family_id";
			System.out.println("sql==" + sql);
			Object[] params = {};

			return super.queryOjects(sql, params);

		} catch (SQLException e) {
			throw new DbException("按级别查找行政区域失败!", e);
		}
	}

	public List<Join_nh> findByKeyValue(String key, String value) throws DbException {
		try {
			String sqlSB = "";
			StringBuffer sb = new StringBuffer(1024);
			if (key != null && value != null) {
				sqlSB = sb.append(" and " + key + "='" + value + "'").toString();
			}
			String sql = "select * from tb_join_nh where 1=1 " + sqlSB + "";

			Object[] params = {};

			return super.queryOjects(sql, params);

		} catch (SQLException e) {
			throw new DbException("慢性病证信息查询失败!", e);
		}
	}
}
